package com.zhengdongzulin.zuke.app.main.apartment

import androidx.lifecycle.rxLifeScope
import com.lxj.androidktx.livedata.StateLiveData
import com.zhengdongzulin.zuke.app.Api
import com.zhengdongzulin.zuke.app.data.City
import com.zhengdongzulin.zuke.app.data.HomeItem
import com.zhengdongzulin.zuke.base.BaseViewModel
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toResponse

class ApartmentVm : BaseViewModel() {

    var get_city_com_list: StateLiveData<MutableList<City>> = StateLiveData()

    fun get_city_com_list() {
        rxLifeScope.launch(
                {
                    var r = RxHttp.postJson(Api.GET_CITY_COM_LIST)
                            .toResponse<MutableList<City>>()
                            .await()
                    get_city_com_list.postValueAndSuccess(r)
                }, {
            assignment(it, get_city_com_list)
        }
        )
    }

    var get_house_item_list: StateLiveData<MutableList<HomeItem>> = StateLiveData()

    /**
     * 排序字段 （1价格 2距离） 默认距离排序 传2
     */
    fun get_house_item_list(cityId: String?, sortType: String) {
        rxLifeScope.launch(
                {
                    var r = RxHttp.postJson(Api.GET_HOUSE_ITEM_LIST)
                            .add("cityId", cityId)
                            .add("wechatId", mShareViewModel!!.apartmentTypeId.value!!)
                            .add("sortType", sortType)
                            .toResponse<MutableList<HomeItem>>()
                            .await()
                    get_house_item_list.postValueAndSuccess(r)
                }, {
            assignment(it, get_house_item_list)
        }
        )
    }

}